package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("CrawlerfilestandardetailService")
@Transactional

public class CrawlerfilestandardetailServiceImpl extends CmiEmaService implements CrawlerfilestandardetailService {
	public CrawlerfilestandardetailServiceImpl() {
	}

	@Transactional
	public Crawlerfilestandardetail saveCrawlerfilestandardetail(Crawlerfilestandardetail crawlerfilestandardetail) {

		if (crawlerfilestandardetail.getId() == null) {
			Crawlerfilestandard crawlerfilestandard = crawlerfilestandardDAO
					.findCrawlerfilestandardById(crawlerfilestandardetail.getCrawlerfilestandard().getId());
			Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
					.findCrawlerfilestandardcolumnById(crawlerfilestandardetail.getCrawlerfilestandardcolumn().getId());

			Crawlerfilestandardetail existingCrawlerfilestandardetail = crawlerfilestandardetailDAO
					.findCrawlerfilestandardetailByPhysicalPrimaryKey(crawlerfilestandard.getId(),
							crawlerfilestandardcolumn.getId(), crawlerfilestandardetail.getNumber());

			crawlerfilestandardetail.setCrawlerfilestandard(crawlerfilestandard);
			crawlerfilestandardetail.setCrawlerfilestandardcolumn(crawlerfilestandardcolumn);

			if (isEquals(crawlerfilestandardetail, existingCrawlerfilestandardetail)) {
				return existingCrawlerfilestandardetail;
			}

			crawlerfilestandardetail = crawlerfilestandardetailDAO.store(crawlerfilestandardetail);
			crawlerfilestandardetailDAO.flush();

		} else {
			Crawlerfilestandardetail existingCrawlerfilestandardetail = crawlerfilestandardetailDAO
					.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId());

			crawlerfilestandardetail = crawlerfilestandardetailDAO.store(crawlerfilestandardetail);
			crawlerfilestandardetailDAO.flush();
		}

		return crawlerfilestandardetail;
	}

	public void deleteCrawlerfilestandardetail(Crawlerfilestandardetail crawlerfilestandardetail) {
		crawlerfilestandardetailDAO.remove(crawlerfilestandardetail);
		crawlerfilestandardetailDAO.flush();
	}

	@Transactional
	public void deleteCrawlerfilestandardetails(Set<Integer> deleteCrawlerfilestandardetailsId) {
		Query query = crawlerfilestandardetailDAO.createNamedQuery("deleteCrawlerfilestandardetailsById", -1, -1);
		query.setParameter(1, deleteCrawlerfilestandardetailsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerfilestandardetail> loadCrawlerfilestandardetails() {
		return crawlerfilestandardetailDAO.findAllCrawlerfilestandardetails();

	}

	@Transactional
	public Set<Crawlerfilestandardetail> loadReferenceCrawlerfilestandardetails(
			Set<Crawlerfilestandardetail> crawlerfilestandardetails) {
		Iterator<Crawlerfilestandardetail> crawlerfilestandardetailsIterator = crawlerfilestandardetails.iterator();
		Set<Crawlerfilestandardetail> referencedCrawlerfilestandardetails = new HashSet<>();
		while (crawlerfilestandardetailsIterator.hasNext()) {
			Crawlerfilestandardetail currentCrawlerfilestandardetail = crawlerfilestandardetailsIterator.next();

		}
		return referencedCrawlerfilestandardetails;
	}

	public Set<Crawlerfilestandardetail> loadCrawlerfilestandardetailsByIdSet(
			Set<Integer> crawlerfilestandardetailIdSet) {

		Iterator<Integer> crawlerfilestandardetailIdIterator = crawlerfilestandardetailIdSet.iterator();
		Set<Crawlerfilestandardetail> crawlerfilestandardetailSet = new HashSet<>();
		while (crawlerfilestandardetailIdIterator.hasNext()) {
			Integer id = crawlerfilestandardetailIdIterator.next();
			Crawlerfilestandardetail currentCrawlerfilestandardetail = crawlerfilestandardetailDAO
					.findCrawlerfilestandardetailById(id);
			crawlerfilestandardetailSet.add(currentCrawlerfilestandardetail);
		}
		return crawlerfilestandardetailSet;
	}

	@Transactional
	public List<Crawlerfilestandardetail> findAllCrawlerfilestandardetails(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerfilestandardetail>(
				crawlerfilestandardetailDAO.findAllCrawlerfilestandardetails(startResult, maxRows));
	}

	@Transactional
	public Crawlerfilestandardetail findCrawlerfilestandardetailByPrimaryKey(Integer id) {
		return crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerfilestandardetails() {
		return ((Long) crawlerfilestandardetailDAO
				.createQuerySingleResult("select count(o) from Crawlerfilestandardetail o").getSingleResult())
						.intValue();
	}

	@Transactional
	public Integer countCrawlerfilestandardetails(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerfilestandardetail.class);
		return ((Long) crawlerfilestandardetailDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countCrawlerfilestandardetailsByName(String queryName) {
		Query query = crawlerfilestandardetailDAO.createNamedQuery("countCrawlerfilestandardetailsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerfilestandardetailsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestandardetail.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerfilestandardetailObjectReferencedSet(
			Crawlerfilestandardetail crawlerfilestandardetail) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (crawlerfilestandardetail.getCrawlerfilestandard() != null) {

			Set<Integer> crawlerfilestandardIds = new HashSet<Integer>();
			crawlerfilestandardIds.add(crawlerfilestandardetail.getCrawlerfilestandard().getId());
			objectReferencedSetMap.put("crawlerfilestandardIds", crawlerfilestandardIds);
		}
		if (crawlerfilestandardetail.getCrawlerfilestandardcolumn() != null) {

			Set<Integer> crawlerfilestandardcolumnIds = new HashSet<Integer>();
			crawlerfilestandardcolumnIds.add(crawlerfilestandardetail.getCrawlerfilestandardcolumn().getId());
			objectReferencedSetMap.put("crawlerfilestandardcolumnIds", crawlerfilestandardcolumnIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerfilestandardetailClassReferencedSet(
			Crawlerfilestandardetail crawlerfilestandardetail) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("crawlerfilestandards", crawlerfilestandardDAO.findAllCrawlerfilestandards());
		classReferencedSetMap.put("crawlerfilestandardcolumns",
				crawlerfilestandardcolumnDAO.findAllCrawlerfilestandardcolumns());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerfilestandardetail crawlerfilestandardetail, Object obj) {
		if (obj == crawlerfilestandardetail)
			return true;
		if (!(obj instanceof Crawlerfilestandardetail))
			return false;
		Crawlerfilestandardetail comparedCrawlerfilestandardetail = (Crawlerfilestandardetail) obj;
		if (!CheckPropertyEqual(crawlerfilestandardetail.getCrawlerfilestandard().getId(),
				comparedCrawlerfilestandardetail.getCrawlerfilestandard().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(crawlerfilestandardetail.getCrawlerfilestandardcolumn().getId(),
				comparedCrawlerfilestandardetail.getCrawlerfilestandardcolumn().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(crawlerfilestandardetail.getNumber(), comparedCrawlerfilestandardetail.getNumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

